//
//  main.cpp
//  Parse
//
//  Created by Benson Tran on 5/21/13.
//  Copyright (c) 2013 Benson Tran. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define SC_NUM 14
#define SC_LENGTH 3101
#define PAT_NUM 3

struct pattern{
    char scan_in[SC_NUM][SC_LENGTH];
    char scan_out[SC_NUM][SC_LENGTH];
    
};

int main(int argc, const char * argv[])
{
    
    pattern* pattern_array[PAT_NUM]; // Create pattern array.
    for (int i = 0; i < PAT_NUM; i++) {
        
        pattern_array[i] = new pattern;

        
    }
    
    string data; // Serves as the string buffer for getline()
    char temp = 0; // Serves as char buffer.
    

     ifstream file (argv[1]); // stil.txt file
    
     if (!file.is_open()) {
         
    	cout << "Error opening file" << endl;
    	return -1;
         
     }
    int counter = 1; // Used to target the first "scan_in1"; Assumes that scan_in1 will always be on the 51st line
                     // In other words, assumes that the header stays constant in all stil files.
    
    while(!file.eof()){
        
    getline(file, data, '\r'); // Consistently moves pointer until "scan_in1"=.
    // cout << counter << endl;
    counter ++;
    // cout << data << endl;
        
        if(counter == 51){
            counter ++;
            
            // Read in the first "scan_in1".
            getline(file, data);
            
            for(int i = 0; i < SC_LENGTH; i++){ // Read in "scan_in1" data.
                file >> pattern_array[0]->scan_in[0][i];
            }
            
            
            for(int k = 0; k <= PAT_NUM; k ++){ // Go through each pattern
                if(k > 0){ // Already read in all pattern 1s "scan_in"'s
                    
                    file >> temp;
                    // cout << temp << endl; // Outputs ;
                    file >> temp;
                    // cout << temp << endl; // Outputs }
                    
                    // Get rid of po and pi data.
                    int equalsigncounter = 0; // Needs to equal 3
                    
                    while(equalsigncounter != 3){
                        file >> temp;
                        // cout << temp;
                        if(temp == '='){
                            equalsigncounter ++;
                            // cout << equalsigncounter << endl;
                        }
                    }
                    

                    // Get scan_out data
                    // int counters = 0; // For debug
                    for(int i = 0; i < SC_NUM; i++){
                        // counters ++; // For debug
                        // cout << counters << endl << endl << endl; // For debug
                        
                        for(int j = 0; j < SC_LENGTH; j++){ // Put scan_out data in pattern array
                            file >> temp;
                            // cout << temp;
     
                            pattern_array[k-1]->scan_out[i][j] = temp;
                            
                        }
                        
                    // Next lines of code is to move pointer.
                        
                    file >> temp;
                    // cout << temp << endl; // Outputs ;
                    file >> temp;
                    // cout << temp << endl; // Outputs "
                    getline(file, data, '\r');
                    // cout << data << endl; // Outputs scan_out"=
                        
                        
                    }
                    
                    for(int i = 0; i < SC_NUM; i++){
                        
                        for(int j = 0; j < SC_LENGTH; j++){ // Put scan_in data in pattern array.

                            file >> temp;
                            if(k != PAT_NUM){ // Discard last scan_in.
                                pattern_array[k]->scan_in[i][j] = temp;
                            }
                            
                        }
                        
                    // Next lines of code is to move pointer.
                        
                    file >> temp;
                    // cout << temp << endl; // Outputs ;
                    file >> temp;
                    // cout << temp << endl; // Outputs "
                    getline(file, data, '\r');
                    // cout << data << endl; // Outputs scan_out"=
                        
                    }
                    
                }
            
                else{ // Read in the first set of scan_ins
                    for(int i = 1; i < SC_NUM; i++){
                        
                        file >> temp;
                        // cout << temp << endl; // Outputs ;
                        file >> temp;
                        // cout << temp << endl; // Outputs "
                        getline(file, data, '\r');
                        // cout << data << endl; // Outputs scan_in"=
                
                        for(int j = 0; j < SC_LENGTH; j++){
                    
                            file >> pattern_array[k]->scan_in[i][j];

                    
                        }
                    }
                }
                
                        


                
                
                    

            }
            
        }
        
    }
    
    file.close();
    
    // Next lines of code are used to check if the parsing was correct.
    
    ofstream myfile;
    myfile.open ("test.txt");
    
    for(int i = 0; i < PAT_NUM; i++){
        
            myfile << "Pattern " << i << ": \n" << "\n";
        
        for(int j = 0; j < SC_NUM; j++){ // Write scan_ins to file
            
            myfile << "scan_in" << j+1 << ": ";
            
            for(int k = 0; k < SC_LENGTH; k++){
                
                myfile << pattern_array[i]->scan_in[j][k];
                
                
            }
            
            myfile << "\n" << "\n";
            
        }
        
       
        
            for(int j = 0; j < SC_NUM; j++){ // Write scan_outss to file
            
                myfile << "scan_out" << j+1 << ": ";
            
                for(int k = 0; k < SC_LENGTH; k++){
                
                    myfile << pattern_array[i]->scan_out[j][k];
                
                
                }
            
                 myfile << "\n" << "\n";
            
            }
        
        
        myfile << "\n" << "\n";
        
        
    }
    
    myfile.close();

    return 0;
     
    
    
}

